package hlhj.fhp.tvlib.adapter

import android.graphics.drawable.AnimationDrawable
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.shuyu.gsyvideoplayer.GSYVideoManager
import hlhj.fhp.tvlib.Contents
import hlhj.fhp.tvlib.R
import hlhj.fhp.tvlib.javabean.CastListNewBean

/**
 * Created by Never Fear   on 2018\9\11 0011.
Never More....
 */
class HomeCastNewFgmAdp(private val datas:ArrayList<CastListNewBean.DataBean.ListBean>,
                        private val hash:HashMap<Int,Boolean>)
    :BaseQuickAdapter<CastListNewBean.DataBean.ListBean,BaseViewHolder>(R.layout.fgm_home_new_cast_item,datas) {
     var listener:OnItemPlay?=null
    override fun convert(helper: BaseViewHolder?, item: CastListNewBean.DataBean.ListBean?) {
        Glide.with(mContext).load(Contents.IMG_URL+item?.radio_thumb).into(helper?.getView(R.id.itemImg)!!)

        val img = helper?.getView<ImageView>(R.id.img)
        val animDrawable= AnimationDrawable()
        animDrawable.addFrame(mContext.resources.getDrawable(R.drawable.voice1),100)
        animDrawable.addFrame(mContext.resources.getDrawable(R.drawable.voice2),100)
        animDrawable.addFrame(mContext.resources.getDrawable(R.drawable.voice3),100)
        animDrawable.addFrame(mContext.resources.getDrawable(R.drawable.voice4),100)
        animDrawable.addFrame(mContext.resources.getDrawable(R.drawable.voice5),100)
        animDrawable.addFrame(mContext.resources.getDrawable(R.drawable.voice6),100)
        animDrawable.isOneShot=false
        img?.background=animDrawable
        if (hash[helper?.adapterPosition]==null){
            animDrawable.stop()
        }else{
            if (hash[helper?.adapterPosition]!!){
                animDrawable.start()
            }else{
                animDrawable.stop()
            }
        }

        helper?.setText(R.id.itemTittle,item?.radio_name)
        helper?.setText(R.id.itemDes,item?.online)
        helper?.setText(R.id.itemTag,"${item?.pm}")

        val itemBt = helper?.getView<ImageView>(R.id.itemBt)
        when(hash[helper?.adapterPosition]){
            true->{
                img?.visibility= View.VISIBLE
                itemBt?.setImageResource(R.drawable.ic_stop_new)

            }
            false->{
                img?.visibility= View.GONE
                itemBt?.setImageResource(R.drawable.ic_play_new)

            }
        }
        itemBt?.setOnClickListener {
            if (hash[helper?.adapterPosition]!!){
                hash.put(helper?.adapterPosition,false)
                GSYVideoManager.releaseAllVideos()
            }else{

                for (i in 0..hash.size-1){
                    if (helper?.adapterPosition==i){
                        hash.put(i,true)
                        listener?.play(i)
                    }else{
                        hash.put(i,false)
                    }
                }
            }

            notifyDataSetChanged()
        }
    }


    interface OnItemPlay{
        fun play(p:Int)
    }
}